﻿using DL.Characters.Player.FSM.StateMachine;
using UnityEngine;

namespace DL.Characters.Player.FSM.State
{
    /// <summary>
    /// 跳跃开始阶段
    /// </summary>
    public class PlayerJumpStartState : PlayerOnAirState
    {
        public PlayerJumpStartState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(
            playerStateMachine, property)
        {
        }

        public override void Enter()
        {
            base.Enter();
            //打断自定义移动 
            StateMachine.Player.CustomMovementController.InterruptCustomMovement();
            ReusableData.InputMult = ReusableData.JumpScalableForwardSpeed;
            ReusableData.IsJump = true;
            ReusableData.CurrentJumpCount = 1;
            Animator.CrossFade("JumpStart", ReusableData.AniTransitionTime);
            // MoveController.IgnoreGravity(0.2f);
            //这里可能会出现问题.尝试将斜面上的跳跃去除水平面的分量添加到水平移动上面去。但是需要重写移动类。有空再说（没空）
            // Vector3 characterUp = MoveController.TryGetGroundNormal();
            // var vspeed = characterUp * JumpUpSpeed;
            //公式计算
            var jumpUpSpeed = Mathf.Sqrt(2 * ReusableData.JumpHight * -MoveController.CharacterGravity);
            //应用向上的速度  
            MoveController.CharacterVerticalVelocityInterface(jumpUpSpeed);
        }

        public override void Update()
        {
            base.Update();
            if (CanFall())
            {
                StateMachine.ChangeState<PlayerAirFallingState>();
            }
        }

        private bool CanFall()
        {
            if (MoveController.CurrentVerticalVelocity.Value < -.5f)
            {
                return true;
            }

            return false;
        }
    }
}